package device

import (

	"iot/iot_app_server/app/util"
	"iot/iot_app_server/proto"
	"common/logging"
	"encoding/json"
	"time"
)

//device发起
func HandleReportGW(pub *MSGBUS_DOWNSTREAM.MqttPub, e error) {
	//显示当前时间
	sTime := time.Now()
	rmap := make(map[string]interface{})
	Deviceid := pub.GetIdentity()
	payloadinfo := &util.PayloadInfo{}
	sendFunc := func(msg []byte) {
		err, _ := util.SendMsgToDevice(Deviceid, util.TOPIC_REPORT_ACK, string(msg), "", 0)
		if err != nil {
			logging.Error("HandleReportGW SendMsgToDevice failed, err=%s", err.Error())
			return
		}
		logging.Debug("HandleReportGW SendMsgToDevice OK")
	}
	//json反解
	err := json.Unmarshal(pub.Msg, payloadinfo)
	//参数验证
	if err != nil ||Deviceid == "" || payloadinfo.Msgid == "" {
		logging.Error("HandleReportGW para error 2",)
		return
	}

	
	logging.Debug("[HandleReportGW]  device[id: %s] ok", Deviceid)

	sendFunc(util.LogGetResponseString_PB(pub, sTime, payloadinfo.Msgid, util.ERR_CODE_SUCCESS, rmap))
	logging.Info("HandleReportGW  OK")

}
